package com.hutao.medical.controller;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hutao.medical.common.config.ConfigProperties;
import com.hutao.medical.common.util.CookieUtils;
import com.hutao.medical.common.util.Result;
import com.hutao.medical.constant.FunctionType;
import com.hutao.medical.pojo.quality.HospitalNote;
import com.hutao.medical.pojo.vo.OperatorImgVo;
import com.hutao.medical.pojo.vo.QueryScanVo;
import com.hutao.medical.service.BasicDataService;
import com.hutao.medical.service.ScanService;
import com.hutao.medical.service.quality.QualityService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
@RequestMapping("/scan")
public class ScanController {

	private static final Logger logger = LoggerFactory.getLogger(ScanController.class);
	@Autowired
	private ConfigProperties configProperties;
	
	@Autowired
	private ScanService scanService;
	
	@Autowired
	private BasicDataService basicDataService;
	
	@Autowired
	private QualityService qualityService;


	@RequestMapping("/index")
	public String index(Model model) {
		Map<String, List<java.util.Map<String, Object>>> map=basicDataService.getBaseInfoMap();
		if(MapUtils.isNotEmpty(map)){
			model.addAttribute("officeList",map.get("offices"));
			model.addAttribute("outHospitalTypeList", map.get("outHospitalList"));
		}
		return "scan/scanAdd";
	}
	
	@RequestMapping("/scanSearch")
	public String scanSearch(Model model) {
		Map<String, List<java.util.Map<String, Object>>> map=basicDataService.getBaseInfoMap();
		if(MapUtils.isNotEmpty(map)){
			model.addAttribute("officeList",map.get("offices"));
			model.addAttribute("outHospitalList", map.get("outHospitalList"));
			model.addAttribute("sexes", map.get("sexes"));	
		}
		return "scan/scanSearch";
	}

	
	


	@RequestMapping("/searchScan")
	public String searchScan(String firstGuid,Model model) {
		HospitalNote hospitalNote = qualityService.getHospitalNote(firstGuid,FunctionType.scan);
		model.addAttribute("hospitalNote", hospitalNote);
		List<Map<String,Object>> scanFileList=scanService.getScanFileListByFirstGuid(firstGuid);
		model.addAttribute("scanFileList", scanFileList);
		model.addAttribute("scanFileSize", scanFileList.size());
		System.out.println(scanFileList);
		model.addAttribute("picHostPath", configProperties.getServer().getPicHostPath());
		return "scan/scan";
	}
	
	
	@RequestMapping("/photoPicPage")
	public String photoPicPage(String firstGuid,Model model) {
		HospitalNote hospitalNote = qualityService.getHospitalNote(firstGuid,FunctionType.scan);
		model.addAttribute("hospitalNote", hospitalNote);
		List<Map<String,Object>> scanFileList=scanService.getScanFileListByFirstGuid(firstGuid);
		model.addAttribute("scanFileList", scanFileList);
		model.addAttribute("picHostPath", configProperties.getServer().getPicHostPath());
		return "scan/photoPicPage";
	}

	

	/**
	 * 旋转图片
	 * @param queryScanVo
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/rotateImg")
	public Result rotateImg(QueryScanVo queryScanVo){
		Result result = null;
		logger.info("--------------进入图片旋转操作-----------------");
		try {
			scanService.rotateImg(queryScanVo);
			result = new Result(true,"图片旋转成功!");
		} catch (Exception e) {
			result = new Result(false);
			logger.error("操作图片旋转出现异常:异常信息="+e.getMessage());
		}
		return result;
	}
	
	
	/**
	 * 跳转到图片裁剪页面
	 * @param model
	 * @param picUrl
	 * @return
	 */
	@RequestMapping("/toImgCut")
	public String toImgCut(Model model,String picUrl){
		model.addAttribute("picUrl", picUrl);
		return "scan/picCut";
	}
	
	/**
	 * 图片裁剪
	 * @param xwidth
	 * @param ywidth
	 * @param width
	 * @param height
	 * @param picUrl
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cutImg")
	public Result cutImg(OperatorImgVo operatorImgVo){
		Result result = null;
		try {
			scanService.imageCut(operatorImgVo);
			result = new Result(true);
		} catch (Exception e) {
			result = new Result(false);
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 删除扫描图片
	 * @param queryScanVo
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteScanImg")
	public Result deleteScanImg(QueryScanVo queryScanVo){
		logger.info("------进入删除扫描图片----");
		Result result = null;
		try {
			
			result=scanService.deleteScanFile(queryScanVo);
			
		} catch (Exception e) {
			e.printStackTrace();
			result=new Result(false,"删除扫描图片出现异常");
		}
		return result;
	}
	
	/**
	 * 扫描图片并上传
	 * @param queryScanVo
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/uploadScanImgs")
	public Result uploadScanImgs(QueryScanVo queryScanVo,HttpServletRequest request){
		logger.info("------------进入扫描图片数据上传-----------");
		Result result = null;
		try {
			 String operator = CookieUtils.getCookieValue(request, "userName");
			 result = scanService.uploadScanFileByHttpClient(queryScanVo, operator);
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("扫描图片上传出现错误:错误信息"+e.getMessage());
		}
		return result;
	}
	
	
	
	@RequestMapping("/medRepair")
	public String medRepair(){
		return "medControl/medRepair";
	}
	
	@RequestMapping("/medDefect")
	public String medDefect(Model model){
		Map<String, List<Map<String, Object>>> map = basicDataService.getBaseInfoMap();	
		model.addAttribute("officeList", map.get("offices"));
		return "medControl/medDefect";
	}
	
	
	
	@RequestMapping("scanTest")
	public String scanTest(){
		return "scan/scanTest";
	}
	
	@RequestMapping("shortTermstatistic")
	public String shortTermstatistic(){
		return "scan/shortTermstatistic";
	}
	
	@RequestMapping("/paginationWorkStatistic")
	public String paginationWorkStatistic() {
		return "scan/paginationWorkStatistic";
	}
	
	
	
	

	

	
	

}




